﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Upkeeps;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Upkeeps
{
    public class UpkeepRepository : DapperDbContextRepository, IUpkeepRepository
    {
        public UpkeepRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示保养记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        /// <param name="cardnumber"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<UpkeepDto>> GetShowUpkeep(string name, DateTime? time, string cardnumber, int pageindex, int pagesize)
        {
            PageResponse<UpkeepDto> response = new PageResponse<UpkeepDto>();
            var sql = $"select a.*,b.License from Upkeep a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(name))
            { 
                sql += $" and a.UpkeepManager like '%" + name + "%'";
            }
            if (time != null)
            {
                sql += $" and a.UpkeepTime='{time}'";
            }
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            var list = await this.QueryAsync<UpkeepDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<UpkeepDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除保养记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelUpkeep(int id)
        {
            var sql = $"update Upkeep set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除保养记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsUpkeep(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Upkeep set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="upkeepDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateUpkeep(UpkeepDto upkeepDto)
        {
            var sql = $"update Upkeep set Title='{upkeepDto.Title}',CarNumber='{upkeepDto.CarNumber}',UpkeepMoney='{upkeepDto.UpkeepMoney}',UpkeepManager='{upkeepDto.UpkeepManager}',UpkeepProject='{upkeepDto.UpkeepProject}',UpkeepTime='{upkeepDto.UpkeepTime.ToString("yyyy/MM/dd")}',Remark='{upkeepDto.Remark}' where Id='{upkeepDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加保养记录
        /// </summary>
        /// <param name="upkeepDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddUpkeep(UpkeepDto upkeepDto)
        {
            var sql = $"insert into Upkeep values('{upkeepDto.UpkeepNumber}','{upkeepDto.Title}','{upkeepDto.CarNumber}','{upkeepDto.UpkeepMoney}','{upkeepDto.UpkeepManager}','{upkeepDto.UpkeepKM}','{upkeepDto.UpkeepTime.ToString("yyyy/MM/dd")}','{upkeepDto.UpkeepProject}','{upkeepDto.ShangUpkeepTime.ToString("yyyy/MM/dd")}','{upkeepDto.ShangUpkeepKM}','{upkeepDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, upkeepDto);
            return list;
        }
    }
}
